package g2.routemaster.gui.actions;

import g2.routemaster.gui.BoardSingleton;
import g2.routemaster.gui.GManager;
import g2.routemaster.gui.general.ECityCard;
import g2.routemaster.model.Branch;
import g2.routemaster.model.City;
import g2.routemaster.model.GameManager;
import g2.routemaster.model.Player;
import g2.routemaster.model.Region;

import java.util.Iterator;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.window.ApplicationWindow;

public class Test2Action extends Action {
   private ApplicationWindow window;
   public Test2Action(ApplicationWindow window) {
      setText("Auto Flags Last Round@Ctrl+Alt+2");
      setToolTipText("Test Action");
      this.window = window;
   }
   
   public void run() {
      situationEndGame();
      BoardSingleton.instance().updateAll();
   }
   
   void situationEndGame(){
      GameManager gm = GManager.instance().getGameManager();
      Player playerKaia = gm.getFirstPlayer();
      Player playerMaie = gm.getNextPlayer(playerKaia);
      
      addFlag(gm, playerKaia, ECityCard.CARLSRUHE.nom());
      addFlag(gm, playerKaia, ECityCard.FREIBURG.nom());
      addFlag(gm, playerKaia, ECityCard.BASEL.nom());
      addFlag(gm, playerKaia, ECityCard.SIGMARINGEN.nom());
      addFlag(gm, playerKaia, ECityCard.STUTTGARD.nom());
      addFlag(gm, playerKaia, ECityCard.ULM.nom());
      addFlag(gm, playerKaia, ECityCard.KEMPTEN.nom());
      addFlag(gm, playerKaia, ECityCard.AUGSBURG.nom());
      addFlag(gm, playerKaia, ECityCard.INGOLSTADT.nom());
      addFlag(gm, playerKaia, ECityCard.NYRNBERG.nom());
      addFlag(gm, playerKaia, ECityCard.REGENSBURG.nom());
      addFlag(gm, playerKaia, ECityCard.PASSAU.nom());
      addFlag(gm, playerKaia, ECityCard.MYNCHEN.nom());
      addFlag(gm, playerKaia, ECityCard.LODZ.nom());
      addFlag(gm, playerKaia, ECityCard.ZYRICH.nom());
      addFlag(gm, playerKaia, ECityCard.SALZBURG.nom());
      addFlag(gm, playerKaia, ECityCard.BUDWEIS.nom());
      addFlag(gm, playerKaia, ECityCard.PILSEN.nom());

      addFlag(gm, playerMaie, ECityCard.CARLSRUHE.nom());
      addFlag(gm, playerMaie, ECityCard.STUTTGARD.nom());
      addFlag(gm, playerMaie, ECityCard.ULM.nom());
      addFlag(gm, playerMaie, ECityCard.SIGMARINGEN.nom());
      addFlag(gm, playerMaie, ECityCard.BASEL.nom());
      addFlag(gm, playerMaie, ECityCard.ZYRICH.nom());
      addFlag(gm, playerMaie, ECityCard.WYRZBURG.nom());
      addFlag(gm, playerMaie, ECityCard.INGOLSTADT.nom());
      addFlag(gm, playerMaie, ECityCard.AUGSBURG.nom());
      addFlag(gm, playerMaie, ECityCard.KEMPTEN.nom());
      addFlag(gm, playerMaie, ECityCard.INNSBRUCK.nom());
      addFlag(gm, playerMaie, ECityCard.MYNCHEN.nom());
      addFlag(gm, playerMaie, ECityCard.PASSAU.nom());
      addFlag(gm, playerMaie, ECityCard.REGENSBURG.nom());
      addFlag(gm, playerMaie, ECityCard.NYRNBERG.nom());

   }
   
   void addFlag(GameManager gm, Player player, String cityName){
      Branch branch = player.getFreeBranch();
      branch.setCity(getCity(gm, cityName));
   }
   
   private City getCity(GameManager gm, String cityName){
      Iterator iterRegion = gm.getBoard().iteratorOfRegion();
      while(iterRegion.hasNext()){
         Region region = (Region)iterRegion.next();
         Iterator iterCity = region.iteratorOfCity();
         while(iterCity.hasNext()){
            City city = (City)iterCity.next();
            if(city.getName().equals(cityName)){
               return city;
            }
         }
      }
      return null;
   }
   
}
